<template>
  <div class="searchResults_container">
    <van-nav-bar
      :title="`${$route.query.keywords} 的搜索结果`"
      left-arrow
      @click-left="$router.back()"
      fixed
      placeholder
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push({ path: '/article', query: { id: item.art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchList } from "./home";
export default {
  name: "searchResults",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    };
  },
  methods: {
    async onLoad() {
      let res = await getSearchList({
        page: this.page,
        per_page: 10,
        q: this.$route.query.keywords
      });
      this.page++;
      // 异步更新数据
      this.list.push(...res.data.data.results);

      // 加载状态结束
      this.loading = false;

      // 数据全部加载完成
      if (res.data.data.results.length == 0) {
        this.finished = true;
      }
    }
  }
};
</script>

<style></style>
